//
//  ViewController.m
//  加载网络图片
//
//  Created by 曹小泽的mac on 2016/10/26.
//  Copyright © 2016年 曹小泽的Mac. All rights reserved.
//

#import "ViewController.h"
#import <UIImageView+WebCache.h>
#import <MJRefresh.h>
#import <ASIHTTPRequest.h>

#define URL_ @"http://m2.qiushibaike.com/article/list/imgrank?count=30&page=%ld"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSMutableArray * _imgArr;
    NSMutableArray * _itemsArr;
    NSInteger num;
}
@property(nonatomic,strong) UITableView *tableView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    _imgArr = [NSMutableArray array];
    _itemsArr = [NSMutableArray array];
    
    [self.view addSubview:self.tableView];
    num =1;
    
    //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        num =1;
        [self getPageData:num];
    }];
    //上拉刷新
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        //每拉一次数据加1
        num +=1;
        
        [self getPageData:num];
    }];
    
    
}
-(void)getPageData:(NSInteger )PageNum
{
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:URL_,num]];
    ASIHTTPRequest *request =[[ ASIHTTPRequest alloc]initWithURL:url];
    ASIHTTPRequest *newRequest = request;
    [request setCompletionBlock:^{
        
        NSData *data = newRequest.responseData;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        _itemsArr = [dic objectForKey:@"items"];
        for (NSDictionary * imgDic in _itemsArr) {
            NSString *imgStr = imgDic[@"image"];
            
            NSString *firstStr = [imgStr substringWithRange:NSMakeRange(3, 5)];
            NSString *secondStr = [imgStr substringWithRange:NSMakeRange(3, 9)];
            NSString *urlStr = [NSString stringWithFormat:@"http://pic.qiushibaike.com/system/pictures/%@/%@/medium/%@",firstStr,secondStr,imgStr];
            
            [_imgArr addObject:urlStr];
        }
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
    }];
    //异步开始请求
    [request startAsynchronous];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _imgArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str=@"ds";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:str];
    if(cell ==nil)
    {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
    
    NSString *imgStr = _imgArr[indexPath.row];
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:imgStr] placeholderImage:[UIImage imageNamed:@"屏幕快照 2016-10-26 19.26.55"]];
    
    return cell;
}
-(UITableView *)tableView
{
    if(_tableView ==nil)
    {
        _tableView=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
        _tableView.delegate=self;
        _tableView.dataSource=self;
        _tableView.rowHeight= 80;
    }
    return _tableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
